package cn.jinjimi.app.main.fragment;

import android.support.v4.view.ViewPager;

import cn.jinjimi.base.preference.PreferenceManager;
import cn.jinjimi.base.util.StringUtils;
import cn.jinjimi.framework.helper.annotation.EventTrace;
import cn.jinjimi.framework.helper.annotation.EventTraceParam;
import cn.jinjimi.app.R;
import cn.jinjimi.app.bean.EventDataField;
import cn.jinjimi.app.bean.EventID;
import cn.jinjimi.app.bean.BundleKey;
import cn.jinjimi.app.main.adapter.FragmentPageAdapter;
import cn.jinjimi.app.main.dialog.SceneGuideDialog;
import cn.jinjimi.app.main.extend.BaseNetControlFragment;
import cn.jinjimi.app.main.widget.PagerSlidingTabStrip;

import java.util.ArrayList;
import java.util.List;

import static cn.jinjimi.app.bean.SharePrefKeys.PREF_SCENE_CARD_OPEN_FIRST;

/**
 * Created by lforxeverc on 16/9/27.
 */

public class CardContainerFragment extends BaseNetControlFragment implements ViewPager.OnPageChangeListener {
    private ViewPager mViewPager;
    private PagerSlidingTabStrip indicator;

    private int jumpPosition = 0;

    @Override
    protected int getBizLayoutResourceId() {
        return R.layout.fragment_card_home;
    }

    @Override
    protected void initialBiz() {
        initView();
        initFragments();
    }

    @Override
    protected void loadData() {
        netLoadSucc();
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    }

    @Override
    @EventTrace(EventID.APP_CARD_TAB)
    public void onPageSelected(@EventTraceParam(value = EventDataField.ParamPostion, isValueAutoPlusOne = true) int position) {
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    private void initView() {
        mViewPager = (ViewPager) findView().findViewById(R.id.vp_cards);
        indicator = (PagerSlidingTabStrip) findView().findViewById(R.id.indicator_tabs);
        indicator.setOnPageChangeListener(this);

        String tab = getActivity().getIntent().getStringExtra(BundleKey.PARAM_TAB);
        if (!StringUtils.isEmpty(tab)) {
            try {
                jumpPosition = Integer.parseInt(tab);
            } catch (NumberFormatException e){
                e.printStackTrace();
            }
        }

        if (!PreferenceManager.getBooleanData(PREF_SCENE_CARD_OPEN_FIRST)) {
            PreferenceManager.setData(PREF_SCENE_CARD_OPEN_FIRST, true);
            SceneGuideDialog dialog = new SceneGuideDialog(getActivity());
            dialog.show();
        }
    }

    private void initFragments() {
        List<CardFragment> fmList = new ArrayList<>();

        CardFragment birthday = new CardFragment();
        birthday.setTitle(getString(R.string.card_birthday));
        birthday.setType(CardFragment.LoadType.BIRTHDAY);
        fmList.add(birthday);

        CardFragment holiday = new CardFragment();
        holiday.setTitle(getString(R.string.card_holiday));
        holiday.setType(CardFragment.LoadType.HOLIDAY);
        fmList.add(holiday);

        CardFragment scene = new CardFragment();
        scene.setTitle(getString(R.string.card_scene));
        scene.setType(CardFragment.LoadType.SCENE_LIST);
        fmList.add(scene);

        FragmentPageAdapter adapter = new FragmentPageAdapter(getChildFragmentManager(), fmList);
        mViewPager.setAdapter(adapter);
        indicator.setViewPager(mViewPager);

        jumpPosition = jumpPosition >= adapter.getCount() - 1 ? adapter.getCount() : jumpPosition;
        mViewPager.setCurrentItem(jumpPosition);
    }
}
